//
//  NumberSprite.m
//  StarBugs
//
//  Created by ?? ? on 2/14/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//


#include "Sprite/NumberSprite.h"
#include "Util/ModuleController.h"

using namespace cocos2d;

NumberSprite::NumberSprite(int number, int type, int _align, bool plus, ccColor3B color)
{
    mainSprite = new CCSprite();
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    numberColor = color;
    switch(type)
    {
        case number_type_1:
            strcpy(txt, "num_4-score1.png");
            width = 14;// * ModuleController::getImageScale();
            height = 16;// * ModuleController::getImageScale();
            break;
        case number_type_2:
            strcpy(txt, "num_4-score2.png");
            width = 16;// * ModuleController::getImageScale();
            height = 18;// * ModuleController::getImageScale();
            break;
        case number_type_3:
            strcpy(txt, "num_1.png");
            width = 14;// * ModuleController::getImageScale();
            height = 18;// * ModuleController::getImageScale();
            break;
        case number_type_result:
            strcpy(txt, "num_result.png");
            width = 28;// * ModuleController::getImageScale();
            height = 32;// * ModuleController::getImageScale();
            break;
            
    }
    m_align = _align;
    m_plus = plus;
        
    setNumber(number);
}

NumberSprite::~NumberSprite()
{
    
}

void NumberSprite::setNumber(int num)
{
    char tmp[16];
    sprintf(tmp, "%d", num);
    int cnt = 0;
    CCSprite *sprite;
    int _num = num;
    
    while (true) {
        _num = _num / 10;
        cnt ++;
        if(_num == 0)
            break;
    }
    if(m_plus)
    {
        int totalwidth = (width - 2) * (cnt + 1);
        
        for(int i = 0; i < cnt; i++)
        {
            int _out = tmp[i] - 48;
            sprite = CCSprite::create(txt, CCRectMake(_out * width * ModuleController::getImageScale(), 0,
                                                              width * ModuleController::getImageScale(), height * ModuleController::getImageScale()));
            sprite->setPosition(ccp((((width - 2) * (i + 1)) + (width - 2)/2) - totalwidth/2, 0));
            sprite->setColor(numberColor);
            mainSprite->addChild(sprite);
        }
        
        sprite = CCSprite::create(txt, CCRectMake(10 * width * ModuleController::getImageScale(), 0,
                                                          width * ModuleController::getImageScale(), height * ModuleController::getImageScale()));
        sprite->setPosition(ccp(((width - 2)/2) - totalwidth/2, 0));
        sprite->setColor(numberColor);
        mainSprite->addChild(sprite);
    }
    else
    {
        int totalwidth = (width - 2) * cnt;
        
        for(int i = 0; i < cnt; i++)
        {
            int _out = tmp[i] - 48;
            sprite = CCSprite::create(txt, CCRectMake(_out * width * ModuleController::getImageScale(), 0,
                                                              width * ModuleController::getImageScale(), height * ModuleController::getImageScale()));
            if(m_align == align_left)
            {
                sprite->setPosition(ccp((((width - 2) * i) + (width - 2)/2), 0));
            }
            else if(m_align == align_center)
            {
                sprite->setPosition(ccp((((width - 2) * i) + (width - 2)/2) - totalwidth/2, 0));

            }
            else
                sprite->setPosition(ccp((((width - 2) * i) + (width - 2)/2) - totalwidth, 0));
            sprite->setColor(numberColor);
            mainSprite->addChild(sprite);

        }
    }
}

//void NumberSprite::setNumber(int num)
//{
//    setNumber(num, m_align, m_plus);
//}

void NumberSprite::_call()
{
    removeChild(mainSprite, false);
    removeFromParentAndCleanup(false);
    
}
void NumberSprite::startAni(CCPoint pt)
{
    mainSprite->setPosition(pt);
    mainSprite->setOpacity(0);
    
    CCFadeIn *fade = CCFadeIn::create(0.1);
    CCMoveBy *move = CCMoveBy::create(0.2, ccp(0, 20 * ModuleController::getImageScale()));
  
//    CCScaleTo *scale1 = CCScaleTo::create(0.1, 1.0f);
    CCDelayTime *delay = CCDelayTime::create(0.8f);
    CCScaleTo *scale2 = CCScaleTo::create(0.05, 1.5, 0.5);
    CCScaleTo *scale3 = CCScaleTo::create(0.05, 0.5, 0.5);
    CCScaleTo *scale4 = CCScaleTo::create(0.05, 0);
    CCCallFunc *call = CCCallFunc::create(this, callfunc_selector(NumberSprite::_call));
    CCAction *action = CCSequence::create(CCSpawn::create(fade, move,NULL ), delay, scale2, scale3, scale4, call, NULL);
    
    mainSprite->runAction(action);
}

void NumberSprite::startCountAni(cocos2d::CCPoint pt, int pre, int last)
{
    preScore = pre;
    lastScore = last;

    timer = 0;

    schedule(schedule_selector(NumberSprite::countTick));
    
}

//void NumberSprite::setNumberColor(ccColor3B color)
//{
//    mainSprite->setColor(color);
//}

void NumberSprite::countTick(float dt)
{
    int _tmp = lastScore - preScore;
    score = preScore + _tmp * timer;
    timer += dt;
    if(timer > 1)
    {
        unschedule(schedule_selector(NumberSprite::countTick));
        score = lastScore;
    }
    mainSprite->removeAllChildrenWithCleanup(false);
//    CCLog("!%d",m_plus);
    m_plus = false;
    setNumber(score);
}

